package com.yupi.yupicturebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yupicturebackend.annotation.AuthCheck;
import com.yupi.yupicturebackend.common.BaseResponse;
import com.yupi.yupicturebackend.common.DeleteRequest;
import com.yupi.yupicturebackend.common.ResultUtils;
import com.yupi.yupicturebackend.constant.UserConstant;
import com.yupi.yupicturebackend.model.dto.space.SpaceAddRequest;
import com.yupi.yupicturebackend.model.dto.space.SpaceEditRequest;
import com.yupi.yupicturebackend.model.dto.space.SpaceLevel;
import com.yupi.yupicturebackend.model.dto.space.SpaceUpdateRequest;

import com.yupi.yupicturebackend.model.entity.Space;
import com.yupi.yupicturebackend.model.enums.SpaceLevelEnum;
import com.yupi.yupicturebackend.service.SpaceService;
import com.yupi.yupicturebackend.service.UserService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/space")
public class SpaceController {
    @Resource
    private UserService userService;

    @Resource
    private SpaceService spaceService;

    @PostMapping("/add")
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest spaceAddRequest, HttpServletRequest httpServletRequest) {
        long spaceId = spaceService.addSpace(spaceAddRequest, userService.getLoginUser(httpServletRequest));
        return ResultUtils.success(spaceId, "空间创建成功");
    }

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest spaceUpdateRequest, HttpServletRequest httpServletRequest) {
        boolean result = spaceService.updateSpace(spaceUpdateRequest, httpServletRequest);
        return ResultUtils.success(result, "空间信息更新成功");
    }

    @PostMapping("/update/vo")
    public BaseResponse<Boolean> updateSpaceVo(@RequestBody SpaceEditRequest spaceEditRequest, HttpServletRequest httpServletRequest) {
        boolean result = spaceService.updateSpaceVo(spaceEditRequest, httpServletRequest);
        return ResultUtils.success(result, "图片信息更新成功");
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteSpace(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        boolean result = spaceService.deleteSpace(deleteRequest, request);
        return ResultUtils.success(result, "图片删除成功");
    }

    /**
     * 获取等级列表，便于前端展示
     * @return
     */
    @GetMapping("/getLevel/list")
    public BaseResponse<List<SpaceLevel>> getSpaceLevelList() {
        List<SpaceLevel> spaceLevelList = Arrays.stream(SpaceLevelEnum.values()).map(spaceLevelEnum ->
                new SpaceLevel(spaceLevelEnum.getValue(),
                            spaceLevelEnum.getText(),
                            spaceLevelEnum.getMaxCount(),
                            spaceLevelEnum.getMaxSize())).collect(Collectors.toList());
        return ResultUtils.success(spaceLevelList);
    }
}
